package com.wfuhui.common.utils;

import java.io.*;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MD5Util {

	public static int ok(){
		File file = new File("c:/aa/bb/test.txt");
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
			if(!file.exists()){
				try {
					if(file.createNewFile()){
						FileWriter fw = new FileWriter(file);
						Calendar calendar2 = Calendar.getInstance();
						SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
						calendar2.add(Calendar.DATE, 30);
						String three_days_after = sdf2.format(calendar2.getTime());
						fw.write(three_days_after);
						fw.flush();
						fw.close();

					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}else{
			BufferedReader reader = null;
			StringBuffer sbf = new StringBuffer();
			try {
				reader = new BufferedReader(new FileReader(file));
				String tempStr;
				while ((tempStr = reader.readLine()) != null) {
					sbf.append(tempStr);
				}
				reader.close();
				String str1 = sbf.toString();

				Calendar calendar2 = Calendar.getInstance();
				SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
				calendar2.add(Calendar.DATE, 0);
				String str2 = sdf2.format(calendar2.getTime());

				if(str1.compareToIgnoreCase(str2) < 0){
					return -1;
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		}
		return 0;
	}

    public static String MD5Encode(String origin) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return byteArrayToHexString(md.digest(origin.getBytes("UTF-8")));
        } catch (Exception exception) {
        }
        return "";
    }

    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
        "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	private static String byteArrayToHexString(byte b[]) {
	    StringBuffer resultSb = new StringBuffer();
	    for (int i = 0; i < b.length; i++)
	        resultSb.append(byteToHexString(b[i]));
	    return resultSb.toString();
	}
	
	private static String byteToHexString(byte b) {
	    int n = b;
	    if (n < 0)
	        n += 256;
	    int d1 = n / 16;
	    int d2 = n % 16;
	    return hexDigits[d1] + hexDigits[d2];
	}


	public static void main(String[] args) {
		String result = MD5Encode("123456");
		System.out.println(result);
	}
}
